package com.yoomiito.app.utils

import android.app.Activity
import android.content.Context
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.RelativeLayout
import android.widget.TextView
import com.alibaba.fastjson.JSONObject
import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.chuanglan.shanyan_sdk.listener.OneKeyLoginListener
import com.chuanglan.shanyan_sdk.listener.OpenLoginAuthListener
import com.chuanglan.shanyan_sdk.tool.ShanYanUIConfig
import com.umeng.socialize.bean.SHARE_MEDIA
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.net.NetUrl

/**
 * 闪验工具类
 */
object ShanYanUtils {
    var isInitShanYan = false //是否初始化闪验
    var isGetPhoneInfo = false //是否预取号
    fun initShanYan(context: Context) {
        //appkey:4YPFI8pl  appid:p2hwP4R9
        if (!App.mAppConfig.isLogin) {
            OneKeyLoginManager.getInstance().init(context, "p2hwP4R9") { code, result ->
                isInitShanYan = 1022 == code
                L.i("闪验初始化结果：$code   $result")
            }
            OneKeyLoginManager.getInstance().getPhoneInfo { code, result ->
                isGetPhoneInfo = 1022 == code
                L.i("闪验预取号结果：$code   $result")
            }
        }
    }

    fun openLoginAuth(context: Context,result: (Boolean, String) -> Unit) {
        OneKeyLoginManager.getInstance().setAuthThemeConfig(UIConfig(context))
        OneKeyLoginManager.getInstance().openLoginAuth(false,
                OpenLoginAuthListener { p0, p1 ->
                    if (1000 != p0) {
                        result(false, "")
                    }
                },
                OneKeyLoginListener { p0, p1 ->
                    when (p0) {
                        1000 -> {
                            val result = JSONObject.parseObject(p1)
                            val token = result.getString("token")
                            L.i("闪验Token：$token")
                            result(true, token)
                        }
                        1011 -> {
                            OneKeyLoginManager.getInstance().finishAuthActivity()
                        }//点击闪验返回按钮 包括物理返回按键
                        else -> {
                            result(false, "")
                        }
                    }
                })
    }

    private fun UIConfig(context: Context):ShanYanUIConfig {

        val otherTV = TextView(context)
        otherTV.text = "使用其他号码登录"
        otherTV.setTextColor(ResUtil.getColor(R.color.color_balk_333333))
        otherTV.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 17f)
        val mLayoutParams1 = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        mLayoutParams1.setMargins(0, DensityUtil.dp2px(290f), 0, 0)
        mLayoutParams1.addRule(RelativeLayout.CENTER_HORIZONTAL)
        otherTV.layoutParams = mLayoutParams1

        val wxView = LayoutInflater.from(context).inflate(R.layout.item_wx_login, null, false)
        val mLayoutParams3 = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
        mLayoutParams3.setMargins(0, DensityUtil.dp2px(400f), 0, 0)
        mLayoutParams1.addRule(RelativeLayout.CENTER_HORIZONTAL)
        wxView.layoutParams = mLayoutParams3


        return ShanYanUIConfig.Builder()
                .setNavText("登录")
                .setNavTextSize(18)
                .setNavTextColor(ResUtil.getColor(R.color.color_text_030303))
                .setNavReturnImgPath(ResUtil.getDrawable(R.drawable.icon_back_2))
                .setNavReturnBtnWidth(24)
                .setNavReturnBtnHeight(24)
                .setLogoImgPath(ResUtil.getDrawable(R.drawable.icon_logo_radius))
                .setLogBtnImgPath(ColorUtils.gradientAndRadius("#FFFF246C", "#FFFF035B",23f))
                .setNumberColor(ResUtil.getColor(R.color.color_black))
                .setNumberSize(18)
                .setSloganTextColor(ResUtil.getColor(R.color.color_text))
                .setSloganTextSize(12)
                .setLogBtnText("本机联网号码一键登录")
                .setLogBtnTextSize(17)
                .setLogBtnTextColor(ResUtil.getColor(R.color.color_white))
                .setLogBtnHeight(46)
                .setLogBtnWidth(327)
                .addCustomView(otherTV,false,false){_, _ ->
                    LoginUtils.toLogin(context)
                }
                .addCustomView(wxView,false,false){_, _ ->
                    LoginUtils.WXLogin(context as Activity, SHARE_MEDIA.WEIXIN)
                }
                .setAppPrivacyOne("用户协议",NetUrl.UserAgreementUrl)
                .setAppPrivacyTwo("隐私协议",NetUrl.PrivacyAgreementUrl)
                .setAppPrivacyColor(ResUtil.getColor(R.color.color_balk_999999),ResUtil.getColor(R.color.color_balk_333333))
                .setPrivacySmhHidden(false)
                .setCheckBoxHidden(true)
                .setPrivacyTextSize(12)
                .setPrivacyText("登录即同意","和","","","并授权千年艾获取本机号码")
                .build()
    }
}